package com.macasa.panel.entity.impl;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import org.hibernate.annotations.ForeignKey;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.IDomain;
import com.macasa.panel.entity.api.IPureFtpdService;
import com.macasa.panel.entity.api.IPureFtpdServiceUser;
import com.macasa.panel.entity.api.ISGBDServiceDatabase;

@Entity
@Table(name = "pure_ftpd_service")
@Getter
@Setter
@ToString(callSuper = true)
public class PureFtpdService extends Service implements IPureFtpdService {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long			serialVersionUID	= 7856558294337013197L;

	@OneToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, targetEntity = com.macasa.panel.entity.impl.SGBDServiceDatabase.class)
	@JoinColumn(name = "fk_sgbd_service_database", nullable = false, unique = false)
	@ForeignKey(name = "fk_pure_ftpd_service_01")
	private ISGBDServiceDatabase		sgbdDatabase;

	@Column(name = "ssl", nullable = false)
	private Boolean						ssl;

	@Column(name = "key", nullable = true)
	private Integer						key;

	@OneToMany(mappedBy = "pureFtpdService", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.PureFtpdServiceUser.class)
	@ForeignKey(name = "fk_pure_ftpd_service_user_01")
	private List<IPureFtpdServiceUser>	users;

	@OneToMany(mappedBy = "pureFtpdService", cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Domain.class)
	@ForeignKey(name = "fk_domain_02")
	private List<IDomain>				domains;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof PureFtpdService)) {
			return false;
		}

		final PureFtpdService other = CastHelpers.cast(obj, PureFtpdService.class);

		return super.equals(obj) && Objects.equal(this.sgbdDatabase, other.sgbdDatabase) && Objects.equal(this.ssl, other.ssl)
				&& Objects.equal(this.key, other.key) && Objects.equal(this.users, other.users) && Objects.equal(this.domains, other.domains);
	}

	@Override
	public int hashCode() {
		return super.hashCode() + Objects.hashCode(this.sgbdDatabase, this.ssl, this.key, this.users, this.domains);
	}
}
